package com.lyp.lease.web.admin.controller.apartment;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyp.lease.common.result.Result;
import com.lyp.lease.model.entity.LabelInfo;
import com.lyp.lease.model.enums.ItemType;
import com.lyp.lease.web.admin.service.LabelInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.function.Consumer;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/admin/label")
public class LabelController {

    @Autowired
    LabelInfoService labelInfoService;

    public static void main(String[] args) {
//        Consumer<LabelInfo> consumer = LabelInfo::getType;
//        System.out.println(consumer);
//        LabelInfo labelInfo = new LabelInfo();
//        labelInfo.setType(ItemType.APARTMENT);
//
//        ItemType type = labelInfo.getType();
//        System.out.println(type);
//        System.out.println(type.getCode());
//        System.out.println(type.getName());
        String code = "2";
        for (ItemType type : ItemType.values()) {
            if (type.getCode().equals(Integer.parseInt(code)))
                System.out.println(type);
        }
    }

    /**
     *
     * @param type 比如：http:localhost/admin/label/list?type=1
     * @return
     */
    @Operation(summary = "（根据类型）查询标签列表")
    @GetMapping("list")
    public Result<List<LabelInfo>> labelList(@RequestParam(value = "type", required = false) ItemType type) {

        LambdaQueryWrapper<LabelInfo> labelInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelInfoLambdaQueryWrapper.eq(type != null, LabelInfo::getType, type);

        //QueryWrapper<LabelInfo> labelInfoQueryWrapper = new QueryWrapper<>();
        //labelInfoQueryWrapper.eq(type != null, "type", type);

        List<LabelInfo> list = labelInfoService.list(labelInfoLambdaQueryWrapper);


        return Result.ok(list);
    }

    @Operation(summary = "新增或修改标签信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdateLabel(@RequestBody LabelInfo labelInfo) {
        labelInfoService.saveOrUpdate(labelInfo);
        return Result.ok();
    }

    @Operation(summary = "根据id删除标签信息")
    @DeleteMapping("deleteById")
    public Result deleteLabelById(@RequestParam Long id) {
        labelInfoService.removeById(id);
        return Result.ok();
    }
}
